﻿Public Class FormFiltreLookup

    Private Sub ButtonOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonOk.Click
        Me.DialogResult = DialogResult.OK
        Me.Close()
    End Sub

    Private Sub ButtonAnnuler_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonAnnuler.Click
        Me.DialogResult = DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Tester()
    End Sub

    Private Sub Tester()
        'Montrer la sélection en marquant le texte
        Dim oMarqueur As New ClassMarqueur()
        oMarqueur.sMasque = Me.ComboBoxFindRegex.Text
        oMarqueur.CoulFondPrimaire = "#FFFFDD"
        oMarqueur.CoulFondSecondaire = "#FFFF00"
        oMarqueur.Marquer(Me.RichTextBoxEssai)
        'Montrer le texte de référence (étape intermédiaire)
        Me.RichTextBoxRef.SelectAll()
        Me.RichTextBoxRef.SelectionColor = System.Drawing.Color.Black
        Me.RichTextBoxRef.SelectionBackColor = System.Drawing.Color.White
        For Each oMatch As System.Text.RegularExpressions.Match In System.Text.RegularExpressions.Regex.Matches(Me.RichTextBoxEssai.Text, Me.ComboBoxFindRegex.Text, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
            'On construit la requete lookup en remplacant les $x dans sLookupRegEx
            oMarqueur.sMasque = Me.TextBoxLookupRegex.Text
            For i = 0 To oMatch.Groups.Count - 1
                oMarqueur.sMasque = oMarqueur.sMasque.Replace("$" & i, oMatch.Groups(i).Value)
            Next 'i
            oMarqueur.MarquerCumulatif(Me.RichTextBoxRef)
        Next
        'Faire le remplacement par référence proprement dit
        Me.RichTextBoxResult.Text = Commun.LookupRef(Me.RichTextBoxEssai.Text, ComboBoxFindRegex.Text, Me.TextBoxReplace.Text, Me.TextBoxLookupRegex.Text, Me.RichTextBoxRef.Text, Me.TextBox1.Text)
        'Dim oTrans As New ClassTextOp
        'oTrans.Operation = ClassTextOp.EnumTextOperation.Lookup
        'oTrans.Parametres(0) = Me.ComboBoxFindRegex.Text
        'oTrans.Parametres(1) = Me.TextBoxReplace.Text
        'oTrans.Parametres(2) = Me.TextBoxLookupRegex.Text
        'oTrans.Parametres(3) = Me.RichTextBoxRef.Text
        'oTrans.Parametres(4) = Me.TextBox1.Text
        'Me.RichTextBoxResult.Text = oTrans.Transformer(Me.RichTextBoxEssai.Text)
        'oTrans = Nothing
    End Sub

    Private Sub FormFiltreLookup_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        Me.ComboBoxFindRegex.Items.Clear()
        Dim oMarqueur As ClassMarqueur
        For Each oItem In Form1.ListViewMarqueurs.Items
            oMarqueur = oItem.Tag
            Me.ComboBoxFindRegex.Items.Add(oMarqueur.Masque)
        Next
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim bExemple As Boolean = True
        If Me.ComboBoxFindRegex.Text & Me.RichTextBoxEssai.Text & Me.TextBoxLookupRegex.Text & Me.RichTextBoxRef.Text & Me.TextBoxReplace.Text & Me.TextBox1.Text <> "" Then
            bExemple = (MsgBox("Remplacer vos paramètres par un exemple ?", MsgBoxStyle.Question) = MsgBoxResult.Ok)
        End If
        If bExemple Then
            Me.RichTextBoxEssai.Text = "R1=100" & vbCrLf & "R2=200" & vbCrLf & "R3=300"
            Me.ComboBoxFindRegex.Text = "(R\d)=(\d+)"
            Me.TextBoxLookupRegex.Text = "$1=(\w+)"
            Me.RichTextBoxRef.Text = "R1=Quebec" & vbCrLf & "R2=Montreal"
            Me.TextBoxReplace.Text = "$$1=$2"
            Me.TextBox1.Text = "Inconnu($1)=$2"
            Me.Tester()
        End If
    End Sub

    Private Sub Button12_Click(sender As System.Object, e As System.EventArgs) Handles Button12.Click
        FormRegEx.TextBoxRegEx.Text = Me.ComboBoxFindRegex.Text
        FormRegEx.RichTextBoxTest.Text = RichTextBoxEssai.Text
        If FormRegEx.ShowDialog = DialogResult.OK Then
            Me.ComboBoxFindRegex.Text = FormRegEx.TextBoxRegEx.Text
            RichTextBoxEssai.Text = FormRegEx.RichTextBoxTest.Text
        End If
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        FormRegEx.TextBoxRegEx.Text = Me.TextBoxLookupRegex.Text
        FormRegEx.RichTextBoxTest.Text = Me.RichTextBoxRef.Text
        If FormRegEx.ShowDialog = DialogResult.OK Then Me.TextBoxLookupRegex.Text = FormRegEx.TextBoxRegEx.Text
    End Sub

    Private Sub ComboBoxDocuments_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBoxDocuments.SelectedIndexChanged
        Dim bRemplacer As Boolean = True
        If Me.RichTextBoxRef.Text <> "" Then bRemplacer = (MsgBox("Remplacer le contenu de référence par celui du fichier " & Me.ComboBoxDocuments.Text, MsgBoxStyle.OkCancel) = MsgBoxResult.Ok)
        If bRemplacer Then
            Me.RichTextBoxRef.Text = Form1.ListViewFichiers.Items(Me.ComboBoxDocuments.Text).Tag.Contenu
        End If
    End Sub

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        Call Me.Tester()
    End Sub

    Private Sub Button5_Click_1(sender As System.Object, e As System.EventArgs) Handles Button5.Click
        Call Me.Tester()
    End Sub

    Private Sub Button6_Click(sender As System.Object, e As System.EventArgs) Handles Button6.Click
        Call Me.Tester()
    End Sub
End Class